package com.smasher.widget.base

import android.view.LayoutInflater
import android.view.View
import androidx.annotation.LayoutRes
import androidx.viewbinding.ViewBinding
import java.lang.reflect.ParameterizedType

/**
 * @author matao
 * @version 1.0
 */
abstract class BaseBindingActivity<T : ViewBinding> : BaseActivity() {

    open lateinit var mBinding: T

    @Suppress("UNCHECKED_CAST")
    override fun getRootView(): View {
        val type = javaClass.genericSuperclass
        if (type is ParameterizedType) {
            val clazz = type.actualTypeArguments[0] as Class<*>
            val method = clazz.getMethod("inflate", LayoutInflater::class.java)
            mBinding = method.invoke(null, layoutInflater) as T
            return mBinding.root
        }
        return LayoutInflater.from(this).inflate(getRootViewRes(), null)
    }


    /**
     * 进行获取根部的View
     *
     * @return RootView
     */
    @LayoutRes
    fun getRootViewRes(): Int {
        return 0
    }
}